
package br.ucb.cesbdao.janelas;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.regex.PatternSyntaxException;

import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.enuns.EnumFormatos;
import br.ucb.cesbdao.enuns.EnumLabelAndBotao;
import br.ucb.cesbdao.enuns.EnumMensagens;
import br.ucb.cesbdao.enuns.EnumResources;
import br.ucb.cesbdao.enuns.EnumTitulo;
import br.ucb.cesbdao.model.TrabalhadorModel;
import br.ucb.cesbdao.model.impl.TrabalhadorModelImpl;
import br.ucb.cesbdao.modeloTabelas.ModeloTabelaUsuario;
import br.ucb.cesbdao.servicos.Servicos;
import br.ucb.cesbdao.visao.Visao;

@SuppressWarnings("serial")
public class JanelaListaUsuarios extends javax.swing.JFrame implements KeyListener{
    
    private ModeloTabelaUsuario modeloTabelaUsuario = new ModeloTabelaUsuario();
    static Logger logger = Servicos.carregaLog(JanelaListaUsuarios.class);
    
    public JanelaListaUsuarios() throws ClassNotFoundException, SQLException, ParseException {
        TrabalhadorModel model = new TrabalhadorModelImpl();
    	initComponents();
        setResizable(false);
        txt_cpfBuscaTrabalhador.setVisible(false);
        modeloTabelaUsuario.setTrabalhadors(model.consultaAllTrabalhador());
        tabelaTrabalhador.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tabelaTrabalhador.getTableHeader().setReorderingAllowed(false);
        this.setTitle(EnumTitulo.TITULO_USUARIOS.getTitulo());
    }

    private void initComponents() throws ParseException {

        bg_filtrarPorPaciente = new javax.swing.ButtonGroup();
        painelListarTrabalhador = new javax.swing.JPanel();
        painelFiltroTrabalhador = new javax.swing.JPanel();
        painelFiltrarPor = new javax.swing.JPanel();
        rb_filtrarPorCPF = new javax.swing.JRadioButton();
        rb_filtrarPorNome = new javax.swing.JRadioButton();
        painelCamadasFiltrarPor = new javax.swing.JLayeredPane();
        txt_nomeTrabalhador = new javax.swing.JTextField();
        txt_cpfBuscaTrabalhador = new javax.swing.JFormattedTextField();
        painelResultadosTrabalhador = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaTrabalhador = new javax.swing.JTable();
        btn_OK = new javax.swing.JButton();

        bg_filtrarPorPaciente.add(rb_filtrarPorCPF);
        bg_filtrarPorPaciente.add(rb_filtrarPorNome);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBounds(new java.awt.Rectangle(300, 25, 0, 0));

        painelFiltroTrabalhador.setBorder(javax.swing.BorderFactory.createTitledBorder(null, EnumLabelAndBotao.FILTRO.getTexto(), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        

        painelFiltrarPor.setBorder(javax.swing.BorderFactory.createTitledBorder(null, EnumLabelAndBotao.FILTRAR_POR.getTexto(), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); 

        rb_filtrarPorCPF.setText(EnumLabelAndBotao.CPF.getTexto());
        rb_filtrarPorCPF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_filtrarPorCPFActionPerformed(evt);
            }
        });

        rb_filtrarPorNome.setSelected(true);
        rb_filtrarPorNome.setText(EnumLabelAndBotao.NOME.getTexto());
        rb_filtrarPorNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_filtrarPorNomeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout painelFiltrarPorLayout = new javax.swing.GroupLayout(painelFiltrarPor);
        painelFiltrarPor.setLayout(painelFiltrarPorLayout);
        painelFiltrarPorLayout.setHorizontalGroup(
            painelFiltrarPorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelFiltrarPorLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(painelFiltrarPorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rb_filtrarPorCPF)
                    .addComponent(rb_filtrarPorNome))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        painelFiltrarPorLayout.setVerticalGroup(
            painelFiltrarPorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelFiltrarPorLayout.createSequentialGroup()
                .addComponent(rb_filtrarPorCPF)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rb_filtrarPorNome))
        );

        txt_nomeTrabalhador.setBorder(javax.swing.BorderFactory.createTitledBorder(null, EnumLabelAndBotao.NOME.getTexto(), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); 
        txt_nomeTrabalhador.addKeyListener(this);
        
        txt_nomeTrabalhador.setBounds(20, 0, 260, 60);
        painelCamadasFiltrarPor.add(txt_nomeTrabalhador, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txt_cpfBuscaTrabalhador.setBorder(javax.swing.BorderFactory.createTitledBorder(null, EnumLabelAndBotao.CPF.getTexto(), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); 
        txt_cpfBuscaTrabalhador.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter(EnumFormatos.CPF.getFormato())));
        txt_cpfBuscaTrabalhador.addKeyListener(this);
        txt_cpfBuscaTrabalhador.setBounds(20, 0, 170, 60);
        painelCamadasFiltrarPor.add(txt_cpfBuscaTrabalhador, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout painelFiltroTrabalhadorLayout = new javax.swing.GroupLayout(painelFiltroTrabalhador);
        painelFiltroTrabalhador.setLayout(painelFiltroTrabalhadorLayout);
        painelFiltroTrabalhadorLayout.setHorizontalGroup(
            painelFiltroTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelFiltroTrabalhadorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelFiltrarPor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(painelCamadasFiltrarPor, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
        );
        painelFiltroTrabalhadorLayout.setVerticalGroup(
            painelFiltroTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelFiltroTrabalhadorLayout.createSequentialGroup()
                .addGroup(painelFiltroTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(painelFiltrarPor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(painelFiltroTrabalhadorLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(painelFiltroTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            
                            .addComponent(painelCamadasFiltrarPor, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        painelResultadosTrabalhador.setBorder(javax.swing.BorderFactory.createTitledBorder(null, EnumLabelAndBotao.RESULTADOS.getTexto(), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); 

        tabelaTrabalhador.setModel(modeloTabelaUsuario);
        tabelaTrabalhador.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaTrabalhadorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaTrabalhador);

        btn_OK.setIcon(new javax.swing.ImageIcon(getClass().getResource(EnumResources.ICONE_SALVAR.getCaminho()))); 
        btn_OK.setText("OK");
        btn_OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_OKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout painelResultadosTrabalhadorLayout = new javax.swing.GroupLayout(painelResultadosTrabalhador);
        painelResultadosTrabalhador.setLayout(painelResultadosTrabalhadorLayout);
        painelResultadosTrabalhadorLayout.setHorizontalGroup(
            painelResultadosTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelResultadosTrabalhadorLayout.createSequentialGroup()
                .addGroup(painelResultadosTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelResultadosTrabalhadorLayout.createSequentialGroup()
                        .addGap(234, 234, 234)
                        .addComponent(btn_OK))
                    .addGroup(painelResultadosTrabalhadorLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)))
                .addContainerGap())
        );
        painelResultadosTrabalhadorLayout.setVerticalGroup(
            painelResultadosTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelResultadosTrabalhadorLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                .addGap(40, 40, 40)
                .addComponent(btn_OK)
                .addContainerGap())
        );

        javax.swing.GroupLayout painelListarTrabalhadorLayout = new javax.swing.GroupLayout(painelListarTrabalhador);
        painelListarTrabalhador.setLayout(painelListarTrabalhadorLayout);
        painelListarTrabalhadorLayout.setHorizontalGroup(
            painelListarTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelListarTrabalhadorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelListarTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(painelListarTrabalhadorLayout.createSequentialGroup()
                        .addComponent(painelResultadosTrabalhador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(2, 2, 2))
                    .addComponent(painelFiltroTrabalhador, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE))
                .addContainerGap())
        );
        painelListarTrabalhadorLayout.setVerticalGroup(
            painelListarTrabalhadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelListarTrabalhadorLayout.createSequentialGroup()
                .addComponent(painelFiltroTrabalhador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(painelResultadosTrabalhador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelListarTrabalhador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(painelListarTrabalhador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }

    private void tabelaTrabalhadorMouseClicked(java.awt.event.MouseEvent evt) {                                             

    }                                            

    private void btn_OKActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        this.dispose();
    }                                                    

    private void pesquisarTrabalhadorActionPerformed() {                                                       
          String busca = null;
          final TableRowSorter<ModeloTabelaUsuario> sorter = new TableRowSorter<ModeloTabelaUsuario>(modeloTabelaUsuario);
          if(rb_filtrarPorCPF.isSelected())
             busca = Servicos.unFormat(txt_cpfBuscaTrabalhador.getText());
          else
             busca = txt_nomeTrabalhador.getText();
          tabelaTrabalhador.setRowSorter(sorter);
          if (busca.isEmpty())
                sorter.setRowFilter(null);
          else{
              try{
                 sorter.setRowFilter(RowFilter.regexFilter("(?i)"+busca, 0, 1));
              }catch (PatternSyntaxException ex) {
            	  logger.error(ex.getMessage());
                 Visao.showMessageWarning(EnumMensagens.MSG_ALERTA_FILTRAR.getMensagem(), "Aviso do Sistema");
              }
          }
    }                                                      
    private void rb_filtrarPorCPFActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        txt_nomeTrabalhador.setVisible(false);
        txt_cpfBuscaTrabalhador.setText(EnumLabelAndBotao.SEM_TEXTO.getTexto());
        txt_cpfBuscaTrabalhador.setVisible(true);
    }                                                

    private void rb_filtrarPorNomeActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        txt_cpfBuscaTrabalhador.setVisible(false);
        txt_nomeTrabalhador.setVisible(true);
        txt_nomeTrabalhador.setText(EnumLabelAndBotao.SEM_TEXTO.getTexto());
    }                                                 

    private javax.swing.ButtonGroup bg_filtrarPorPaciente;
    private javax.swing.JButton btn_OK;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLayeredPane painelCamadasFiltrarPor;
    private javax.swing.JPanel painelFiltrarPor;
    private javax.swing.JPanel painelFiltroTrabalhador;
    private javax.swing.JPanel painelListarTrabalhador;
    private javax.swing.JPanel painelResultadosTrabalhador;
    private javax.swing.JRadioButton rb_filtrarPorCPF;
    private javax.swing.JRadioButton rb_filtrarPorNome;
    private javax.swing.JTable tabelaTrabalhador;
    private javax.swing.JFormattedTextField txt_cpfBuscaTrabalhador;
    private javax.swing.JTextField txt_nomeTrabalhador;

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		pesquisarTrabalhadorActionPerformed();
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}


}
